package com.demo.my.Dao;

import com.demo.my.Entity.Employee;
import com.demo.my.Util.BaseDAO;
import com.demo.my.Util.DBUtil;

import java.sql.*;
import java.util.List;

public class EmployeeDao extends BaseDAO {
    //高和召
    public Employee getEmployee(String employeeNumber, String password) {
        try {
            Connection connection = DBUtil.getConnection();
            String sql = "select * from employee where employee_number=? and password=?;";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setObject(1, employeeNumber);
            ps.setObject(2, password);
            ResultSet rs = ps.executeQuery();
            rs.next();
            int id = rs.getInt(1);
            int number = rs.getInt(2);
            String name = rs.getString(3);
            String gender = rs.getString(4);
            Date birthday = rs.getDate(5);
            String telephone = rs.getString(6);
            String email = rs.getString(7);
            String address = rs.getString(8);
            String photo = rs.getString(9);
            String edu = rs.getString(10);
            int dnum = rs.getInt(11);
            int pnum = rs.getInt(12);
            Date inTime = rs.getDate(13);
            String pass = rs.getString(14);
            String note = rs.getString(15);
            DBUtil.close(rs, ps, connection);
            return new Employee(id, number, name, gender, birthday, telephone, email, address, photo, edu, dnum, pnum, inTime, pass, note,null,null);
        } catch (SQLException throwables) {
            return null;
        }
    }
    public Employee getEntityById(Integer id) {
        String sql = "select * from employee where id=?";
        Object[] objs = {id};
        try {
            Employee one = getOne(sql, new Employee(), objs);
            return one;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public void updateById(String sql , Object... a) throws Exception {
        BaseDAO dao = new BaseDAO();
        dao.addOrupdOrdel(sql,a);
    }

    public int getMaxNumber() throws SQLException {
        Connection connection = DBUtil.getConnection();
        PreparedStatement ps = connection.prepareStatement("select MAX(employee_number) from employee;");
        ResultSet rs = ps.executeQuery();
        rs.next();
        int maxNumber = rs.getInt(1);
        DBUtil.close(rs,ps,connection);
        return maxNumber;
    }

    public void addEmployee(Employee employee) throws SQLException {
        Connection connection = DBUtil.getConnection();
        PreparedStatement ps = connection.prepareStatement("insert into employee values (null,?,?,?,?,?,?,?,null,?,?,?,?,?,?);");
        ps.setObject(1,employee.getEmployeeNumber());
        ps.setObject(2,employee.getName());
        ps.setObject(3,employee.getGender());
        ps.setObject(4,employee.getBirthday());
        ps.setObject(5,employee.getTelephone());
        ps.setObject(6,employee.getEmail());
        ps.setObject(7,employee.getAddress());
        ps.setObject(8,employee.getEducation());
        ps.setObject(9,employee.getDepartmentNumber());
        ps.setObject(10,employee.getPositionNumber());
        ps.setObject(11,employee.getInTime());
        ps.setObject(12,employee.getPassword());
        ps.setObject(13,employee.getNotes());
        ps.executeUpdate();
        DBUtil.close(null,ps,connection);
    }

    public Employee getEmployeeById(String id) throws Exception {
        String sql="select * from employee where id=?;";
        return getOne(sql,new Employee(),id);
    }

    public void updateEmployee(Employee employee) throws SQLException {
        Connection connection = DBUtil.getConnection();
        PreparedStatement ps = connection.prepareStatement("update employee set name=?,gender=?,birthday=?,telephone=?,email=?,address=?,education=?,department_number=?,position_number=?,password=?,notes=? where employee_number=?;");
        ps.setObject(1,employee.getName());
        ps.setObject(2,employee.getGender());
        ps.setObject(3,employee.getBirthday());
        ps.setObject(4,employee.getTelephone());
        ps.setObject(5,employee.getEmail());
        ps.setObject(6,employee.getAddress());
        ps.setObject(7,employee.getEducation());
        ps.setObject(8,employee.getDepartmentNumber());
        ps.setObject(9,employee.getPositionNumber());
        ps.setObject(10,employee.getPassword());
        ps.setObject(11,employee.getNotes());
        ps.setObject(12,employee.getEmployeeNumber());
        ps.executeUpdate();
        DBUtil.close(null,ps,connection);
    }

    public List<Employee> getEmployeeListByDid(Integer id) throws Exception {
        System.out.println(id);
        String sql="select * from employee where department_number=?;";
        System.out.println(123);
        List<Employee> list = getList(sql, new Employee(), id);
        for (Employee employee : list) {
            System.out.println(employee);
        }
        return list;
    }
}
